//
//  SearchHeaper.m
//  Gegeda_music_MY
//
//  Created by lanou3g on 15/10/31.
//  Copyright © 2015年 周莹. All rights reserved.
//

#define Url @"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=2&device=android&pageId=1&pageSize=20&status=0&tagName=%E6%A6%9C%E5%8D%95%7C%E6%8E%92%E8%A1%8C"
#import "SearchHeaper.h"
#import "JSONAnalysis.h"
#define kUrl @"http://project.lanou3g.com/teacher/UIAPI/MusicInfoList.plist"
#import "SearchViewController.h"
#import "SerchModel.h"
#import "SearchViewController.h"
@interface SearchHeaper ()<UISearchBarDelegate>


// yyjhjsd
@end


static SearchHeaper *share = nil;
@implementation SearchHeaper
+(instancetype)shareInstace{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[SearchHeaper  alloc] init];
          share.dataArray = [NSMutableArray  array];
        share.searchArray = [NSMutableArray  array];
    });
    return share;

}



-(void)loadarray:(NSMutableArray *)array  web:(void(^)())result {

dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSURL *urlstring  = [NSURL URLWithString:Url];
    NSURLRequest *request = [NSURLRequest requestWithURL:urlstring];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        //获取数据
        NSDictionary *dit = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *array = dit[@"list"];
  
        [self.dataArray removeAllObjects];
        for (NSDictionary *dict in array) {
            SerchModel *model = [[SerchModel  alloc] init];
            
            [model  setValuesForKeysWithDictionary:dict];
        
            [self.dataArray  addObject:model];
      
            
        }
         //主线程
        dispatch_async(dispatch_get_main_queue(), ^{

            result();
            
        });

     
        
    }];
    
    
    [task resume];

  
 
});


    
    
}



-(void)lodeSearchWedWithstr:(NSString *)searchSttr withBlock:(void(^)())searchResult{

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
       // 这里没有去掉输入框前后空格
        NSCharacterSet  *whiteSpace = [NSCharacterSet  whitespaceAndNewlineCharacterSet];
        
//        SearchViewController *searchVC = [[SearchViewController alloc] init];
        [searchSttr stringByTrimmingCharactersInSet:whiteSpace];
        //转码
//        NSLog(@"%@", searchVC.searchBar.text);
        NSCharacterSet *set = [NSCharacterSet  characterSetWithCharactersInString:searchSttr];
        NSString *titleString = [searchSttr stringByAddingPercentEncodingWithAllowedCharacters:set];
        
        NSString *urlString = [NSString   stringWithFormat: @"https://openapi.youku.com/v2/searches/video/by_keyword.json?client_id=51efa7f614d5a3ca&keyword=%@",titleString];
        
  
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionDataTask *task =  [session dataTaskWithURL:[NSURL URLWithString:urlString]  completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            
            //获取数据
            NSDictionary *dit = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
         
            NSArray *array = dit[@"list"];
            
            [self.dataArray removeAllObjects];
            for (NSDictionary *dict in array) {
                SerchModel *model = [[SerchModel  alloc] init];
                
                [model  setValuesForKeysWithDictionary:dict];
                
                [self.dataArray  addObject:model];
                
                
            }
            //主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                
                searchResult();
                
            });
            
            
            
        }];
        
        
        [task resume];
        
        
        
    });
    


    }


-(void)searchWedsearchstr:(NSString *)search withBlock:(void(^)())search{

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        // 获取数据
        NSArray *array = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:kUrl]];
        
        //        NSLog(@"%@", array);
        
        // 遍历数组
        for (NSDictionary *dic in array) {
             SerchModel *model = [[SerchModel  alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
            
        }
        // 主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            
         search();
            
        });
        
        
        
        
    });



}



@end
